/**************************** (C) COPYRIGHT 2017 Fortiortech shenzhen *****************************
* File Name          : SPIInit.c
* Author             : Fortiortech  Appliction Team
* Version            : V1.0
* Date               : 10-Apr-2017
* Description        : This file contains SPI initial function used for Motor Control.
***************************************************************************************************
* All Rights Reserved
**************************************************************************************************/


/* Includes -------------------------------------------------------------------------------------*/
#include <FU68xx_2.h>
#include <Myproject.h>

//uint8  p_data;
//static qmi8658_state g_imu;

/*-------------------------------------------------------------------------------------------------
	Function Name :	void SPI_Init(void)
	Description   :	SPI初始化配置
	Input         :	输入说明（详细）
  Output				:	输出说明（详细）
-------------------------------------------------------------------------------------------------*/
void SPI_Init(void)
{
	ClrBit(SPI_CR1, SPIEN);								               // 0,disable SPI;1 enable

/*-------------------------------------------------------------------------------------------------
  SPI管脚配置
	1、禁止UART复用，P06配置为MISO，P05配置为MOSI
	2、禁止比较器输出复用，P07配置为SCLK
-------------------------------------------------------------------------------------------------*/
	ClrBit(PH_SEL, UARTEN);								                 // 0,P06 as GPIO or SPI_MISO,P05 as GPIO or SPI_MOSI;1,P06 and p07 as USART
//    SetBit(PH_SEL, T2SEL | T2SSEL); 
    ClrBit(PH_SEL, T2SEL | T2SSEL);
/*-------------------------------------------------------------------------------------------------
	SPI时钟相位/极性配置
	CPHA = 0, CPOL = 0:上升沿接收，下降沿发送，空闲电平为低
	CPHA = 0, CPOL = 1:上升沿发送，下降沿接收，空闲电平为高
	CPHA = 1, CPOL = 0:上升沿发送，下降沿接收，空闲电平为低
	CPHA = 1, CPOL = 1:上升沿接收，下降沿发送，空闲电平为高
-------------------------------------------------------------------------------------------------*/
//	SetReg(SPI_CR0, CPHA | CPOL, CPOL);
//    SetReg(SPI_CR0, CPHA | CPOL, CPHA | CPOL);
	ClrBit(SPI_CR0, CPHA);	
    ClrBit(SPI_CR0, CPOL);	


/*-------------------------------------------------------------------------------------------------
	SPI从方式选择配置
	00：3线从方式或3线主方式，NSS信号不连到端口管脚
	01：4线从方式或4线多主方式，NSS配置为输入
	1x：4线单主方式，NSS配置为输出，NSS信号输出x电平
-------------------------------------------------------------------------------------------------*/
	SetReg(SPI_CR1, NSSMOD0 | NSSMOD1,/* NSSMOD0 |*/ NSSMOD1);
	SetBit(SPI_CR0, SPIMS);                          // 0:Slave, 1:Master

/*-------------------------------------------------------------------------------------------------
	SPI中断配置
	SPIF：SPI字节传输完成中断标志，硬件置位，软件清零
	WCOL：SPI写冲突中断（发送缓冲器非空时写操作），硬件置位，软件清零
	MODF：SPI方式错误中断（多主方式NSS被拉低，MSTEN和SPIEN被清零）硬件置位，软件清零
	RXOVRN：SPI接收溢出中断（SPI接收缓冲器接收溢出），硬件置位，软件清零
-------------------------------------------------------------------------------------------------*/
	SetReg(SPI_CR1, SPIIF|WCOL|MODF|RXOVR,0x00);					 // SPI所有中断清除
	//IP3

	//SPIIE = 0;                                             // SPI中断使能

   //  SPI_CLK = 0;												                   // Fspi = Fcpu / (2*(SPI_CLK + 1)) = 6MHz
    SPI_CLK=2;//0x0b;//2;   // 4 MHz
    SPIIE = 1;                                               //SPI中断使能
    EA = 1;                                                  //打开总中断

	SPI_DR = 0X00; 
	SetBit(SPI_CR1, SPIEN);						                   // enable SPI
}

/*-------------------------------------------------------------------------------------------------
	Function Name :	void SPI_SendData(uint16 Data, uint8 Channel)
	Description   :	SPI传输数据
	Input         :	Data--传输数据，uint16格式
  Output				:	Channel--传输通道，取值0~3
-------------------------------------------------------------------------------------------------*/
//static void spi_delay(uint8 nCount)
//{
//
//	
//	uint8 j;
//	while(nCount--){
//		for(j=0; j<200; j++);
//		for(j=0; j<100; j++);
//	//	for(j=0; j<102; j++);
//	}
//	
//	
////	_nop_();_nop_();_nop_();_nop_();
////	_nop_();_nop_();_nop_();_nop_();
////	_nop_();_nop_();_nop_();_nop_();
//}

//void Delay500ms(void)                                         //@24.000MHz
//{
//    uint8 waitTime = 46;
//    uint8 waitTime1 = 153;
//    uint8 waitTime2 = 245;
//    
//    do
//    {
//        do
//        {
//            while (--waitTime2);
//        } while (--waitTime1);
//    } while (--waitTime);
//}

void Spi_Enable(void)
{
	//	uint8 i;
	ClrBit(SPI_CR1, NSSMOD0);   
	delay_ms(delay_time_1);
//    for ( i = 0; i < 10; i++);
}

void Spi_Disable(void)
{
//	 uint8 i;
	SetBit(SPI_CR1, NSSMOD0);   
    delay_ms(delay_time_1);
//    for ( i = 0; i < 10; i++);
}
#if 0
uint8 Spi_Putch(uint8 pda)
{
	uint8 tmp;
	while (ReadBit(SPI_CR0, SPIBSY));	

	{
		
	//	ClrBit(SPI_CR1, NSSMOD0);
		SPI_DR = pda;
	//	SPI_DR = Data;
	//	SetBit(SPI_CR1, NSSMOD0);
	}
//	while (ReadBit(SPI_CR0, SPIBSY));	

//	delay_ms(2);
//
//	tmp = SPI_DR;
    tmp=1;
	
    return (tmp );
}
#else
uint8 Spi_Putch(uint8 pda)
{
	uint8 count;
	uint8 temp;

	count =0;
	temp =0;

	while (1 ){
		temp= ReadBit(SPI_CR0, SPIBSY) ;//ReadBit(SPI_CR1, TXBMT);		

	    if ((temp==0) ||  (count >=20))
			break;
		delay_us(1);
		count ++;		
	}

	SPI_DR = pda;

	delay_us(20);

	count =0;
	temp =0;

	while (1 ){
		temp= ReadBit(SPI_CR0, SPIBSY);//ReadBit(SPI_CR1, TXBMT);		

	    if ((temp==0) ||  (count >=20))
			break;
		delay_us(1);
		count ++;		
	}

	temp = SPI_DR;
	
    return temp;
}

#endif 

//uint8 Spi_Getch(uint8 *pda){
//	uint8 count;
//	uint8 temp;
//
//	count =0;
//	temp =0;
//
//	while (1 ){	
//
//		temp= ReadBit(SPI_CR1, TXBMT);	
//	    if ((temp==1) ||  (count >=20))
//			break;
//		
//		delay_us(1);
//		count ++;	
//		
//	}
//
//	*pda = SPI_DR;  
//	
//    return 1;
//}


#if 0
uint8 SPI_Read(uint8 Addr, uint8 *pData, uint8 Length)
{
  uint8 i = 0;
	
//	uint16_t i = 0;
//
//	SPI_QST_CS_LOW();
//	spi_delay(1);

//	Spi_SendByte(Addr|0x80);
//	for(i =0; i < Length; i++){
//	  pData[i] = Spi_SendByte(0xff);
//	}
//	spi_delay(1);
//	SPI_QST_CS_HIGH();
    ClrBit(SPI_CR1, NSSMOD0);
//	SetBit(SPI_CR1, NSSMOD0);

	spi_delay(2);



	

	SPI_SendData(Addr|0x80 );
	for(i =0; i < Length; i++){
		SPI_SendData(0xff  );
		spi_delay(2);
	
	  //  while(ReadBit(SPI_CR0, RXBMT));
		pData[i] =  SPI_DR;
	    
	}
//	p_data = SPI_DR;
	spi_delay(2);

//	ClrBit(SPI_CR1, NSSMOD0);

	SetBit(SPI_CR1, NSSMOD0);

	
	return 1;
}
#endif
//void SPI_SendData(uint8 Data)
//{
////	if(!ReadBit(SPI_CR0, SPIBSY))
//	while (ReadBit(SPI_CR0, SPIBSY));	
//
//	{
//		
//	//	ClrBit(SPI_CR1, NSSMOD0);
//		SPI_DR = Data;
//	//	SPI_DR = Data;
//	//	SetBit(SPI_CR1, NSSMOD0);
//	}
//}

//uint8 SPI_Write(uint8 Addr, uint8 Data)
//{
////	SPI_QST_CS_LOW();
//	ClrBit(SPI_CR1, NSSMOD0);
//
//	spi_delay(2);	
//	
//	SPI_SendData(Addr&0x7f);		// write commond
//	SPI_SendData(Data);
//	
//	spi_delay(2);
//
//	SetBit(SPI_CR1, NSSMOD0);
//	
////	SPI_QST_CS_HIGH();
//
//	return 1;
//}


